PROJECTENGRAM

STRUCTS

Topic 3- Structs

Tags: #◇CCPROG2 #◇C Programming #◇Programming #◇Computer Science #◇Data Structures #◇Data Abstraction #◇Data Types #◇Searching #◇Structured Data #◇Structured Programming #◇Memory Management


◇Overview

A struct (structure) in C groups related variables of different types under a single name. It is the primary mechanism for creating custom data types.

◇Key Concepts

struct groups heterogeneous data into one unit Members are accessed with . (direct) or -> (pointer) typedef creates an alias to avoid repeating struct keyword Structs can contain arrays, other structs, and pointers

◇Details / Theory

Defining a Struct

『』
C
struct Student {
    char name[50];
    int age;
    float gpa;
};

// Declaring a variable
struct Student s1;

Using typedef

『』
C
typedef struct {
    char name[50];
    int age;
    float gpa;
} Student;

Student s1;  // No need for 'struct' keyword

Initialization

『』
C
Student s1 = {"Alice", 20, 3.85};

// Designated initializers (C99)
Student s2 = {.name = "Bob", .gpa = 3.50, .age = 21};

Accessing Members

『』
C
// Direct access with dot operator
printf("Name: %s\n", s1.name);
printf("GPA: %.2f\n", s1.gpa);

// Pointer access with arrow operator
Student *ptr = &s1;
printf("Age: %d\n", ptr->age);  // Equivalent to (*ptr).age

Arrays of Structs

『』
C
Student class[30];

// Initialize and access
strcpy(class[0].name, "Alice");
class[0].age = 20;
class[0].gpa = 3.85;

// Traverse
for (int i = 0; i < 30; i++) {
    printf("%s: %.2f\n", class[i].name, class[i].gpa);
}

Nested Structs

『』
C
typedef struct {
    int day, month, year;
} Date;

typedef struct {
    char name[50];
    Date birthday;
} Person;

Person p = {"Alice", {15, 3, 2004}};
printf("Birth year: %d\n", p.birthday.year);

Passing Structs to Functions

『』
C
// By value (copies entire struct)
void printStudent(Student s) {
    printf("%s - %.2f\n", s.name, s.gpa);
}

// By pointer (efficient, allows modification)
void updateGPA(Student *s, float newGPA) {
    s->gpa = newGPA;
}

Struct Size and Padding

The compiler may add padding bytes for alignment:

『』
C
struct Example {
    char a;    // 1 byte + 3 padding
    int b;     // 4 bytes
    char c;    // 1 byte + 3 padding
};
// sizeof may be 12, not 6

◇Common Pitfalls

Cannot use == to compare structs — must compare member by member. Cannot directly assign strings to char[] members — use strcpy(). Passing large structs by value is inefficient — use pointers. Forgetting -> when accessing members through a pointer (using . instead).

◇Summary

TL;DR — struct groups variables of different types into a custom type. Use typedef for cleaner syntax. Access members with . (direct) or -> (pointer). Pass by pointer for efficiency. Combine with arrays for collections of records.

Related: ◇What is an Array — ◇Strings — ◇File Handling — ◇Declaration — ◇Element Type Source / Reference:

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

What is the primary purpose of a struct in C?

Q2

Which keyword can be used to create an alias for a struct to avoid repeating the struct keyword?

Q3

How do you access a member of a struct using a direct variable?

Q4

How do you access a member of a struct when you have a pointer to the struct?

Q5

What is the correct syntax for C99 designated initializers for a struct Student?

Q6

Why might the sizeof a struct be larger than the sum of the sizes of its members?

Q7

Which of the following is true about nested structs?

Q8

How should you assign a string to a char[] member inside a struct after initialization?

Q9

What is a key advantage of passing a struct to a function by pointer instead of by value?

Q10

Which common pitfall should be avoided when comparing two structs?